package com.alibaba.cirtus.ots.application.app.ability.facade.v2serviceimpl;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.bcorder.model.dto.PageQuerySalePlanListRequest;
import com.epoch.app.bcorder.model.dto.PageQuerySalePlanListResponse;
import com.epoch.app.bcorder.service.SalesPlanService;
import com.epoch.app.otsapplicationappsdk.monthplanlist.dto.AsyncRequest;
import com.epoch.app.otsapplicationappsdk.monthplanlist.dto.RenderRequest;
import com.epoch.app.otsapplicationappsdk.monthplanlist.service.MonthPlanListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MonthPlanListV2ServiceImpl implements MonthPlanListService {

    private static final Log log  = Log.getLogger(MonthPlanListV2ServiceImpl.class);

    @Autowired
    SalesPlanService salesPlanService;

    @Override
    @FacadeInvoker
    public Object render(RenderRequest renderRequest) {
        JSONObject result = new JSONObject();
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(renderRequest.getRequestBody()));
        result.put("from", jsonObject.getString("from"));
        String bizId = jsonObject.getString("bizId");
        Integer bizType = jsonObject.getInteger("bizType");
        Integer currentPage = jsonObject.getInteger("currentPage");
        Integer pageSize = jsonObject.getInteger("pageSize");
        int limit = 10;
        if (pageSize != null && pageSize > 0) {
            limit = pageSize;
        }
        if (currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        int start = (currentPage - 1) * limit;
        try {
            PageQuerySalePlanListRequest request = new PageQuerySalePlanListRequest();
            request.setBizId(bizId);
            request.setBizType(bizType);
            request.setStart(start);
            request.setLimit(limit);
            log.info("salesPlanService.pageQuerySalePlanList,param:{}",request);
            PageQuerySalePlanListResponse response = salesPlanService.pageQuerySalePlanList(request);
            log.info("salesPlanService.pageQuerySalePlanList,result:{}",JSON.toJSONString(response));
            if (response == null) {
                throw new Exception("未查到结果");
            }
            result.put("success", response.getSuccess());
            result.put("itemList", response.getResult());
            JSONObject page = new JSONObject();
            page.put("pageSize", limit);
            page.put("currentPage", currentPage);
            page.put("total", response.getTotal());
            int totalPage = response.getTotal() / limit;
            if (response.getTotal() % limit > 0) {
                totalPage++;
            }
            page.put("totalPage", totalPage);
            result.put("page", page);
        } catch (Exception e) {
            log.error("MonthPlanListServiceImpl.render",e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @Override
    @FacadeInvoker
    public Object async(AsyncRequest asyncRequest) {
        Object requestBody = asyncRequest.getRequestBody();
        if (requestBody == null) {
            return null;
        }
        JSONObject result = new JSONObject();
        try {
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(requestBody));
            JSONObject fields = jsonObject.getJSONObject("fields");
            result.put("from", fields.getString("from"));
            Integer currentPage = fields.getInteger("currentPage");
            Integer pageSize = fields.getInteger("pageSize");
            int limit = 10;
            if (pageSize != null && pageSize > 0) {
                limit = pageSize;
            }
            if (currentPage == null || currentPage < 1) {
                currentPage = 1;
            }
            int start = (currentPage - 1) * limit;
            PageQuerySalePlanListRequest request = new PageQuerySalePlanListRequest();
            request.setStart(start);
            request.setLimit(limit);
            log.info("salesPlanService.pageQuerySalePlanList,param:{}",request);
            PageQuerySalePlanListResponse response = salesPlanService.pageQuerySalePlanList(request);
            log.info("salesPlanService.pageQuerySalePlanList,result:{}",JSON.toJSONString(response));
            if (response == null) {
                throw new Exception("未查到结果");
            }
            result.put("success", response.getSuccess());
            result.put("itemList", response.getResult());
            JSONObject page = new JSONObject();
            page.put("pageSize", limit);
            page.put("currentPage", currentPage);
            page.put("total", response.getTotal());
            int totalPage = response.getTotal() / limit;
            if (response.getTotal() % limit > 0) {
                totalPage++;
            }
            page.put("totalPage", totalPage);
            result.put("page", page);
        } catch (Exception e) {
            log.error("打印异常", e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
}
